package com.yanqu.road.server.pb.cookboy;

import com.yanqu.road.entity.activity.cookboy.CookBoyMatch;
import com.yanqu.road.entity.activity.cookboy.CookBoyMatchDish;
import com.yanqu.road.entity.activity.cookboy.CookBoyMatchReward;
import com.yanqu.road.entity.activity.cookboy.process2.*;
import com.yanqu.road.entity.activity.cookboy.process3.CookBoy3Table;
import com.yanqu.road.entity.enums.eCookBoyFoodStatusType;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.cookboy.CookBoyProto;
import com.yanqu.road.server.manager.activity.relation.CrossRelationRankMgr;
import com.yanqu.road.server.manager.cookboy.CrossCookBoyActivityMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CrossCookBoyPb {

    public static CookBoyProto.CookBoy3UserRewardSyncMsg.Builder parseCookBoy3UserRewardSyncMsg(int activityId,Map<Long,CookBoyUpUser> cookerMap, Map<Long,Map<Integer, CookBoy3Table>> cookBoyTableMap){
        CookBoyProto.CookBoy3UserRewardSyncMsg.Builder msg = CookBoyProto.CookBoy3UserRewardSyncMsg.newBuilder();
        msg.setActivityId(activityId);
        for(CookBoyUpUser cookBoyUpUser : new ArrayList<>(cookerMap.values())){
            CookBoyProto.CookBoy3UserRewardTempMsg.Builder userMsg = CookBoyProto.CookBoy3UserRewardTempMsg.newBuilder();
            userMsg.setUserId(cookBoyUpUser.getUserId());
            if(cookBoyTableMap.containsKey(cookBoyUpUser.getUserId())){
                int userCount = 0;
                for (CookBoy3Table cookBoy3Table : new ArrayList<>(cookBoyTableMap.get(cookBoyUpUser.getUserId()).values())){
                    userCount += cookBoy3Table.getCookBoy3JoinerMap().size();
                }
                userMsg.setUserCount(userCount);
                userMsg.setBanquetCount(cookBoyTableMap.get(cookBoyUpUser.getUserId()).size());
            }else{
                userMsg.setBanquetCount(0);
                userMsg.setUserCount(0);
            }
            msg.addDataList(userMsg);
        }
        return msg;
    }

    public static CookBoyProto.CrossCookBoyWindUpMsg.Builder parseCrossCookBoyWindUpMsg(int activityId, List<Long> userIdList){
        CookBoyProto.CrossCookBoyWindUpMsg.Builder respMsg = CookBoyProto.CrossCookBoyWindUpMsg.newBuilder();
        respMsg.setActivityId(activityId);
        respMsg.addAllUserIdList(userIdList);
        return respMsg;
    }

    public static CookBoyProto.CrossCookBoy2HorseSyncServerMsg.Builder parseCookBoy2HorseSyncServerMsg(List<HorseRaceLamp> syncList,int activityId){
        CookBoyProto.CrossCookBoy2HorseSyncServerMsg.Builder respMsg = CookBoyProto.CrossCookBoy2HorseSyncServerMsg.newBuilder();
        respMsg.setActivityId(activityId);
        for(HorseRaceLamp horseRaceLamp : syncList){
            CookBoyProto.CrossCookBoy2HorseNoticeMsg.Builder tempMsg = CookBoyProto.CrossCookBoy2HorseNoticeMsg.newBuilder();
            tempMsg.setActivityId(activityId);
            tempMsg.setUserId(horseRaceLamp.getUserId());
            tempMsg.setDishId(horseRaceLamp.getDishId());
            tempMsg.setTime(horseRaceLamp.getTime());
            UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(horseRaceLamp.getUserId());
            if(userBaseInfo == null){
                return null;
            }
            tempMsg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
            respMsg.addNoticeList(tempMsg);
        }
        return respMsg;
    }

    public static CookBoyProto.CrossCookBoy2TableSyncMsg.Builder parseCrossCookBoy2TableSyncMsg(Map<Integer, CookBoy2DishTable> tableMap, int activityId, long serverId, long homeUserId, int dishNum,long integral){
        CookBoyProto.CrossCookBoy2TableSyncMsg.Builder respMsg = CookBoyProto.CrossCookBoy2TableSyncMsg.newBuilder();
        respMsg.setActivityId(activityId);
        respMsg.setUserId(homeUserId);
        CookBoyProto.CookBoy2UserIdTableRespMsg.Builder tableMsg = parseCookBoy2CookerTableRespMsg(tableMap,activityId,serverId,homeUserId,dishNum,integral);
        respMsg.setUserTable(tableMsg);
        return respMsg;
    }

    public static CookBoyProto.CookBoy2TableTempMsg.Builder parseCookBoy2TableTempMsg(CookBoy2DishTable cookBoy2DishTable){
        CookBoyProto.CookBoy2TableTempMsg.Builder tempTableMsg = CookBoyProto.CookBoy2TableTempMsg.newBuilder();
        tempTableMsg.setTableId(cookBoy2DishTable.getNo());
        tempTableMsg.setId(cookBoy2DishTable.getUserDishId());
        tempTableMsg.setOpenTime((int)(cookBoy2DishTable.getOpenTime()/1000));
        tempTableMsg.setOverTime((int)(cookBoy2DishTable.getOverTime()/1000));
        tempTableMsg.setDishId(cookBoy2DishTable.getDishId());
        Map<Long, CookBoy2Joiner> joinerMap = cookBoy2DishTable.getCookBoy2JoinerMap();
        for(CookBoy2Joiner cookBoy2Joiner : new ArrayList<>(joinerMap.values())){
            CookBoyProto.CookBoy2TableJoinerTempMsg.Builder tempJoinerMsg = CookBoyProto.CookBoy2TableJoinerTempMsg.newBuilder();
            tempJoinerMsg.setServerId(cookBoy2Joiner.getServerId());
            CookBoy2Spoon cookBoy2Spoon = CrossCookBoyActivityMgr.getCookBoy2Spoon(cookBoy2DishTable.getActivityId(),cookBoy2Joiner.getGifttId());
            if(cookBoy2Joiner.getRobotId() == 0){
                tempJoinerMsg.setUserId(cookBoy2Joiner.getUserId());
                tempJoinerMsg.setIsRobot(false);
                tempJoinerMsg.setIntegral(cookBoy2Spoon.getUserIntegral());
            }else{
                tempJoinerMsg.setUserId(cookBoy2Joiner.getRobotId());
                tempJoinerMsg.setIsRobot(true);
                tempJoinerMsg.setIntegral(cookBoy2Spoon.getUserIntegral() * CrossCookBoyActivityMgr.getCookBoy2RobotAdd(cookBoy2DishTable.getActivityId()) / 1000);
            }
            tempJoinerMsg.setSpoonId(cookBoy2Joiner.getGifttId());
            UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(cookBoy2Joiner.getUserId());
            if(userBaseInfo != null){
                tempJoinerMsg.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
            }else{
                tempJoinerMsg.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(new UserBaseInfo()));
            }
            tempTableMsg.addJoinerList(tempJoinerMsg);
        }
        return tempTableMsg;
    }

    public static CookBoyProto.CookBoy2UserIdTableRespMsg.Builder parseCookBoy2CookerTableRespMsg(Map<Integer, CookBoy2DishTable> tableMap, int activityId, long serverId, long homeUserId,int dishNum,long addIntegral){
        CrossUserRank crossUserRank = CrossRelationRankMgr.getCrossActivityRankInfo(activityId,serverId,homeUserId);
        long integral = 0;
        if(crossUserRank != null){
            integral = crossUserRank.getValue().longValue();
        }
        int rank = CrossRelationRankMgr.getCrossActivityRank(activityId,serverId,homeUserId);
        CookBoyProto.CookBoy2UserIdTableRespMsg.Builder respMsg = CookBoyProto.CookBoy2UserIdTableRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setIntegral(integral + addIntegral);
        respMsg.setRank(rank);
        respMsg.setDishNum(dishNum);
        for(CookBoy2DishTable cookBoy2DishTable : new ArrayList<>(tableMap.values())){
            CookBoyProto.CookBoy2TableTempMsg.Builder tempTableMsg = parseCookBoy2TableTempMsg(cookBoy2DishTable);
            respMsg.addTableList(tempTableMsg);
        }
        return respMsg;
    }

    public static CookBoyProto.CookBoyPublicTableTempMsg.Builder parseCookBoyPublicTableTempMsg(CookBoyMatch cookBoyMatch){
        CookBoyProto.CookBoyPublicTableTempMsg.Builder respMsg = CookBoyProto.CookBoyPublicTableTempMsg.newBuilder();
        respMsg.setActivityId(cookBoyMatch.getActivityId());
        respMsg.setNo(cookBoyMatch.getNo());
        respMsg.setStartTime((int)(cookBoyMatch.getOpenTime()/1000));
        Map<Integer, CookBoyMatchDish> cookBoyMatchFoodMap = cookBoyMatch.getMatchFoodMap();
        for(CookBoyMatchDish cookBoyMatchFood : cookBoyMatchFoodMap.values()){
            CookBoyProto.CookBoyPublicFoodTempMsg.Builder tempFoodMsg = CookBoyProto.CookBoyPublicFoodTempMsg.newBuilder();
            tempFoodMsg.setFoodId(cookBoyMatchFood.getFoodId());
            tempFoodMsg.setPos(cookBoyMatchFood.getPos());
            tempFoodMsg.setFraction(cookBoyMatchFood.getFraction());
            tempFoodMsg.setUserId(cookBoyMatchFood.getUserId());
            UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(cookBoyMatchFood.getUserId());
            if(userBaseInfo == null){
                return null;
            }
            tempFoodMsg.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
            respMsg.addFoodList(tempFoodMsg);
        }
        return respMsg;
    }


    public static CookBoyProto.CrossCookBoyMatchOverRewardSyncMsg.Builder parseCrossCookBoyMatchOverRewardSyncMsg(int activityId, List<CookBoyMatchReward> rewardList){
        CookBoyProto.CrossCookBoyMatchOverRewardSyncMsg.Builder respMsg = CookBoyProto.CrossCookBoyMatchOverRewardSyncMsg.newBuilder();
        respMsg.setActivityId(activityId);
        for(CookBoyMatchReward cookBoyMatchReward : rewardList){
            CookBoyProto.CrossCookBoyMatchOverUserTempMsg.Builder tempMsg = CookBoyProto.CrossCookBoyMatchOverUserTempMsg.newBuilder();
            tempMsg.setUserId(cookBoyMatchReward.getUserId());
            tempMsg.setNo(cookBoyMatchReward.getNo());
            tempMsg.setRank(cookBoyMatchReward.getRank());
            tempMsg.setReward(cookBoyMatchReward.getIntegral());
            for(CookBoyMatchDish cookBoyMatchDish : cookBoyMatchReward.getCookBoyMatchDishList()){
                CookBoyProto.CookBoyFinishFoodTempMsg.Builder tempDishMsg = CookBoyProto.CookBoyFinishFoodTempMsg.newBuilder();
                tempDishMsg.setId(cookBoyMatchDish.getId());
                tempDishMsg.setFoodId(cookBoyMatchDish.getFoodId());
                tempDishMsg.setFraction(cookBoyMatchDish.getFraction());
                tempDishMsg.setStatus(eCookBoyFoodStatusType.IsOver.getValue());
                tempDishMsg.setNo(cookBoyMatchReward.getNo());
                tempDishMsg.setRank(cookBoyMatchDish.getRank());
                tempDishMsg.setTime(0);//完成菜品的时间，跨服没用
                tempMsg.addFinishFoodList(tempDishMsg);
            }
            respMsg.addInfoList(tempMsg);
        }
        return respMsg;
    }

    public static CookBoyProto.CrossCookBoyMatchSyncMsg.Builder parseCrossCookBoyMatchSyncMsg(int activityId, List<CookBoyMatch> cookBoyMatchList){
        CookBoyProto.CrossCookBoyMatchSyncMsg.Builder respMsg = CookBoyProto.CrossCookBoyMatchSyncMsg.newBuilder();
        respMsg.setActivityId(activityId);
        for(CookBoyMatch cookBoyMatch : cookBoyMatchList){
            CookBoyProto.CookBoyPublicTableTempMsg.Builder tableMsg = parseCookBoyPublicTableTempMsg(cookBoyMatch);
            respMsg.addPublicTable(tableMsg);
        }
        return respMsg;
    }

    public static CookBoyProto.CookBoyPublicTableSyncMsg.Builder parseCookBoyPublicTableSyncMsg(List<CookBoyMatch> cookBoyMatchList){
        CookBoyProto.CookBoyPublicTableSyncMsg.Builder respMsg = CookBoyProto.CookBoyPublicTableSyncMsg.newBuilder();
        for(CookBoyMatch cookBoyMatch : cookBoyMatchList){
            CookBoyProto.CookBoyPublicTableTempMsg.Builder tableMsg = parseCookBoyPublicTableTempMsg(cookBoyMatch);
            respMsg.addTableList(tableMsg);
        }
        return respMsg;
    }


    public static CookBoyProto.CookBoy2UserTableInfoRespMsg.Builder parseCookBoy2RecordRespMsg(CookBoy2DishTable cookBoy2DishTable) {
        CookBoyProto.CookBoy2UserTableInfoRespMsg.Builder respMsg = CookBoyProto.CookBoy2UserTableInfoRespMsg.newBuilder();
        respMsg.setRet(0);
        if(cookBoy2DishTable == null){
            respMsg.setRet(GameErrorCode.E_COOK_BOY2_TABLE_NOT_FOUND);
            return respMsg;
        }
        CookBoyProto.CookBoy2TableTempMsg.Builder tempMsg = parseCookBoy2TableTempMsg(cookBoy2DishTable);
        respMsg.setTable(tempMsg);
        return respMsg;
    }
}
